<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">	
<head>
	<title>Attendance</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="./EldercareMasterPage/_finalstyle.css" rel="stylesheet" />
	<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" type="text/css" href="./jquery/easyui.css">
    <link rel="stylesheet" type="text/css" href="./jquery/icon.css">

<?php
	define('CONFIG_FILE',true);
	include './config.php';

	define('DB_FILE',true);
	require_once './Tool/DB/SqlDBManager.class.php';

	session_start();

	if (empty($_SESSION["username"])||empty($_SESSION["userType"])) {
		Header("Location: ./Index.php");
		exit();
	}else{
		$userType = $_SESSION["userType"];
	}
	
	// set timeout period in seconds
	$inactive = 7200;
	// check to see if $_SESSION['timeout'] is set
	if (isset($_SESSION["timeout"])) {
		$session_life = time() - $_SESSION["timeout"];
		if ($session_life > $inactive) {
			session_destroy();
			Header("Location: Timeout.php");
			exit();
		}
	}

	$_SESSION["timeout"] = time();
?>
<body onLoad="document.nricform.barcode.focus();">
	<div class="banner">
	</div>
	<div id="main">
		<div class="left-wrapper">
			<div class="logo">
				<img src="images/eldercare.gif" alt="logo" width="240px" />
			</div>
			<div class="menu-wrapper">
				<div id="cssmenu">        
					<?php
						if ($userType == "Therapy Assistant") {
							include_once('./Sidebar/_SidebarTA.php');
						} else if ($userType == "Centre Manager") {
							include_once('./Sidebar/_SidebarCM.php');
						} else {
							include_once('./Sidebar/_SidebarSA.php');
						}
					?>                     
				</div> 
				<div class="clear"></div></div>
		</div>
		<div id="content">
			<div class="content2">                 
				<b style="float:right; margin: 0px 0 0 0">  
					<?php
						if (!empty($_SESSION["centreName"])) {
							$Username = $_SESSION["username"];
							$centreName = $_SESSION["centreName"];
							echo "Welcome, " . $Username . " !" . $centreName . " Branch.";
						} else {
							$Username = $_SESSION["username"];
							echo "Welcome, " . $Username;
						}
					?>                        
					<input type="submit" class="btn btn-danger" value="Logout" onclick="parent.location = 'logout.php'">
					<br/><br/>
					<form method="POST" action="./AttendanceHome.php">
						<div class="control-group" align="right">
							<input style="border-radius:10px;width:160px;height:15px"type="text" name="SearchBox" value=" Search..." onfocus="if (this.value = 'Search...') {this.value = '';}"/>
							<input type="submit" button class="btn"name="btnSearch" value="Search"/> 
							<?php
								if(!empty($_GET['errno'])){
									$errno=$_GET['errno'];
									//Invalid search input
									if($errno==1){
										echo "<p style='text-align:right'><font text-align='right' color='red' size='2'>Invalid Search Input.</font></p>";
									}
								}
							?>
						</div>
					</form>
				</b>
				</p>
                <br>
				<div style="float:center">
				<br>
				<h3>Attendance Taking</h3>
				<br>
				<table  cellpadding="0" cellspacing="0" style="width: 990px; height: 100px;">
					<tr>
						<td style="width: 200px;">
							<?php
								require_once './Tool/DB/SqlDBManager.class.php';
								require_once './Tool/Common/PrepareInput.php';

								$sqlDBManager = new SqlDBManager();

								if($userType == "System Admin"){
									echo"<form  method='POST' class='form-horizontal'>
										<div class='control-group'>
										<b>Select a Centre: </b>
										<select name='ddlCentre'>
										<option value='0'selected >All</option>";

									$sql = "SELECT CentreId, CentreName FROM centre";
									$res=$sqlDBManager->queryRows($sql);

									if(!empty($res)){
											for($i=0;$i<count($res);$i++){
												echo "<option value=\"".$res[$i]['CentreId']."\">".$res[$i]['CentreName']."</option>";				
											}
										}
									echo"</select>
										<input type='submit' button class='btn'name='CentreSubmit' value = 'Submit'/>
										</div>
										</form>";

								//}else if($userType=="Therapy Assistant"){
								}else{
									echo "
									<p style='vertical-align:middle;text-align:center'>
									<b>Scan NRIC: </b>
									<form  method='POST' name='nricform'>&nbsp;&nbsp;<input id='barcode'class='tb10'type='text' name='barcode' size='10' maxLength='9'/>
									</form>";
									if(isset($_POST['barcode'])){
										$NRIC=prepareInput($_POST['barcode']);
										if(preg_match("/^[ST][0-9]{7}[JZIHGFEDCBA]$/",$NRIC)||preg_match('/^[FG][0-9]{7}[XWUTRQPNMLK]$/', $NRIC)){
											include_once("Attendance/makeattendance.php");
											echo "<p></p>";
										}else{
											echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>Invalid Singapore NRIC Format.</font>";
										}
									}
									echo "</p>";
								}
							?>
						</td>
						<td rowspan="3" valign="top"align="center"style="width:790px">
							<?php
								if(isset($_POST['CentreSubmit'])){
									$CenterId=prepareInput($_POST['ddlCentre']);
									if(preg_match("/^[0-9]*$/",$CenterId)){
										include_once("Attendance/getattendance.php");
										echo "<p></p>";
									}else{
										print '<script type="text/javascript">';
										print 'alert("Invalid Centre Type.")';
										print '</script>';
										exit();
									}
								}else if(isset($_POST['btnSearch'])){
									include_once('Attendance/searchattendance.php');
									echo "<p></p>";
								}else if(isset($_POST['btnSO'])){
									$NRIC=prepareInput($_POST['btnSO']);
									if(preg_match("/^[ST][0-9]{7}[JZIHGFEDCBA]$/",$NRIC)||preg_match('/^[FG][0-9]{7}[XWUTRQPNMLK]$/', $NRIC)){
										include_once('Attendance/signoutattendance.php');
										echo "<p></p>";
									}else{
										print '<script type="text/javascript">';
										print 'alert("Invalid NRIC Format.")';
										print '</script>';
										exit();
									}
								}else if(isset($_POST['signoutall'])){			
									include_once("Attendance/signoutall.php");
									echo "<p></p>";
								}else{
									include_once("Attendance/getattendance.php");
									echo "<p></p>";
								}
							?>
						</td>
					</tr>		
				</table>
				</div>
			</div>
			<div class="clear"></div>
		</div>
	</div>
	<div id="footer">&copy; 2013 MP22 IIT-IT Temasek Polytechnic</div>
</body>
<script type="text/javascript" src="./jquery/jquery-1.6.min.js"></script>
<script type="text/javascript" src="./jquery/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="./jquery/jquery.easyui.min.js"></script>
<script type='text/javascript'>
	$(function(){ 
		var flag=0;
		$.ajax({
			async : false,
			type : "POST",
			url : "./Attendance/attendanceReport.php", 
			success : function(data) {
				flag=data;
			}
		});	
		if(flag==1){
			flag=0;
			sendEmail();	
		}
	});

	function sendEmail(){ 
		$.messager.confirm('Monthly Attendance Report', 'The Monthly Attendance Report for last month is ready to mail. Do you want to send it to your Email?', function(r) {
			if (r) {
				var win = $.messager.progress({
	                title:'Please waiting',
	                msg:'Processing...'
	            });
	            setTimeout(function(){
	            	$.post('./Attendance/export.php', function(result) {	       		
						if (result.success) {
							$.messager.progress('close');
							$.messager.show({
								title: 'Success',
								msg: "The Monthly Attendance Report has been sent to your email.",
								showType:'fade',
								style:{
									right:'',
									bottom:''
								}
							});
						}else{
							$.messager.progress('close');
							$.messager.show({// show error message
								title: 'Error',
								msg: result.msg,
								showType:'fade',
								style:{
									right:'',
									bottom:''
								}
							});
						}
					}, 'json');
	            },3000)
			}
		}); 	
	}

</script>	
</html>
